// pages/applayRecord/applayRecord.js
let app = getApp()
let request = require('../../utils/request')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    statusBarHeight: app.globalData.statusBarHeight,
    screenHeight: 0,
    pageSize: 20,
    currentPage: 1,
    pageFlag: false,
    applyList: [],
    selectTabIndex: 2
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      screenHeight: wx.getSystemInfoSync().screenHeight,
      selectTabIndex:options.state,
    })
    options.state ? this.getApplyList(false,options.state) : this.getApplyList();
  },
  onShow:function(){
    //进来获取到当前时间   然后跟后台返回列表chargebackTime字段（最晚取消时间）对比，小于最晚时间就显示取消按钮。整体判断是已支付  未入场   最晚取消时间
    let timestamp = Date.parse(new Date());
    this.setData({
      zTime :timestamp
    })
    console.log(timestamp)
  },
  selectTab(e) {
    let index = parseInt(e.currentTarget.dataset.index)
    if (index == this.data.selectTabIndex) {return}
    this.setData({
      selectTabIndex: index,
      currentPage: 1
    })
    this.getApplyList(false,index)
  },
  /**
   * 获取预约列表
   */
  getApplyList(next,status) {
    status = status ? status : 2
    let that = this
    let parmas = {
      pageNum: this.data.currentPage,
      pageSize: this.data.pageSize,
      status
    }
    request.http('/order-reserve/park-reserve-record/get-reserve-record', 'GET', parmas)
      .then(res => {
        let data = res.data
        console.log(res);
        let applyList = that.data.applyList
        let pageFlag
        if (data.code == 200) {
          if (next) {
            if (data.data.records.length > 0) {
              applyList = applyList.concat(data.data.records)
              pageFlag = false
            } else {
              pageFlag = true
            }
          } else {
            applyList = data.data.records
          }
          console.log(this.data.applyList);
          that.setData({
            applyList,
            pageFlag
          })
        }
      })
  },
  /**
   * 取消预约
   */
  cancelApply(e) {
    let _this = this
    console.log(typeof e.currentTarget.dataset.id)
    console.log(e.currentTarget.dataset.id)
    wx.showModal({
      content: '是否取消车位预约，取消后预约服务费将不退回',
      confirmText: '取消预约',
      cancelText: '再想想',
      success(res) {
        if (res.confirm) {
          request.http('/order-reserve/park-reserve-record/cancel-reserve', 
            'POST',
            { reserveRecordId:e.currentTarget.dataset.id }
          ).then(res => {
            let data = res.data
            if (data.code == 200) {
              wx.showToast({ title: '取消预约成功', icon: 'none' })
              //  页码设置为第一页
              _this.setData({ currentPage: 1 })
              //  跳转到取消页面
              _this.selectTab({ currentTarget: { dataset: { index: 10 } } }) 
            }
          })
        }
      }
    })
  },
  /**
   * 触底加载
   */
  scrollTolower() {
    if (this.data.pageFlag) {
      return
    }
    this.setData({
      pageFlag: true,
      currentPage: this.data.currentPage + 1
    })
    this.getApplyList(true)
  },
})